package com.hb.trans.mvp.ui.fragment


import android.annotation.SuppressLint
import com.alibaba.android.arouter.launcher.ARouter
import com.basiclib.base.BaseFragment
import com.hb.trans.R
import com.hb.trans.bean.PeriodPandect
import com.hb.trans.global.ACTIVITY_TRANS_RECORD
import com.hb.trans.mvp.contract.TransInfosContract
import com.hb.trans.mvp.presenter.TransInfosPresenter
import com.jakewharton.rxbinding2.view.clicks
import dagger.android.support.AndroidSupportInjection
import kotlinx.android.synthetic.main.fragment_trans_info.*
import javax.inject.Inject

class TransInfosFragment : BaseFragment<TransInfosPresenter>(), TransInfosContract.View {
    override fun setViewId(): Int {
        return R.layout.fragment_trans_info
    }

    override fun doOnInvisible() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    override fun doOnVisible() {
        TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
    }

    @Inject
    lateinit var mPresenter: TransInfosPresenter
//    lateinit var rootView: View

    companion object {
        fun newInstance(): TransInfosFragment {
            val fragment = TransInfosFragment()
            return fragment
        }
    }

//    override fun initView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
//        rootView = inflater.inflate(R.layout.fragment_trans_info, container, false)
//        return rootView
//    }

    override fun onFragmentInject() {
        AndroidSupportInjection.inject(this)
    }

    override fun attachView() {
        mPresenter.attachView(this)
    }

    override fun initView() {
    }

    @SuppressLint("CheckResult")
    override fun setListener() {
        super.setListener()
        rlPartOne.clicks().subscribe { startTransRecord(1) }
        rlPartTwo.clicks().subscribe { startTransRecord(2) }
        rlPartThree.clicks().subscribe { startTransRecord(3) }
        rlPartFour.clicks().subscribe { startTransRecord(4) }
    }

    override fun setupData(data: MutableList<PeriodPandect>?) {
        data?.let {
            for (pd in data) {
                when (pd.subject) {
                    1 -> pbOne.setProgress(percentage(pd.lastinstancetime, pd.lastinstancetime + pd.needtime))
                    2 -> pbTwo.setProgress(percentage(pd.lastinstancetime, pd.lastinstancetime + pd.needtime))
                    3 -> pbThree.setProgress(percentage(pd.lastinstancetime, pd.lastinstancetime + pd.needtime))
                    4 -> pbFour.setProgress(percentage(pd.lastinstancetime, pd.lastinstancetime + pd.needtime))
                }
            }
        }
    }

    private fun percentage(valid: Int, total: Int): Int {
        return ((valid.toFloat() / total) * 100).toInt()
    }

    private fun startTransRecord(part: Int) {
        ARouter.getInstance().build(ACTIVITY_TRANS_RECORD)
            .withInt("part", part)
            .navigation()
    }

    override fun initData() {
    }
}
